<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="五大设计原则 S O L I DS-单一职责原则  一个程序只做好一件事 如果功能过于复杂就拆分开, 每个部分保持独立  O-开放封闭原则  对扩展开放, 对修改封闭 增加需求时, 扩展新代码, 而非修改已有代码 这是软件设计的终极目标  L-里氏置换原则  子类能覆盖父类 父类能出现的地方子类就能出现 JS中使用较少 ( 弱类型 &amp; 继承使用较少 )  I-接口独立原则  保持接口的单一">
<meta property="og:type" content="article">
<meta property="og:title" content="js设计模式">
<meta property="og:url" content="https://gz1234.gitee.io/2022/08/02/js%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/index.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content="五大设计原则 S O L I DS-单一职责原则  一个程序只做好一件事 如果功能过于复杂就拆分开, 每个部分保持独立  O-开放封闭原则  对扩展开放, 对修改封闭 增加需求时, 扩展新代码, 而非修改已有代码 这是软件设计的终极目标  L-里氏置换原则  子类能覆盖父类 父类能出现的地方子类就能出现 JS中使用较少 ( 弱类型 &amp; 继承使用较少 )  I-接口独立原则  保持接口的单一">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-08-02T06:54:17.000Z">
<meta property="article:modified_time" content="2023-04-03T09:03:05.916Z">
<meta property="article:author" content="郭泽">
<meta property="article:tag" content="js设计模式">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/2022/08/02/js%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/2022/08/02/js%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/","path":"2022/08/02/js设计模式/","title":"js设计模式"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>js设计模式 | 郭泽</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%94%E5%A4%A7%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99-S-O-L-I-D"><span class="nav-number">1.</span> <span class="nav-text">五大设计原则 S O L I D</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">工厂模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BB%BA%E9%80%A0%E6%A8%A1%E5%BC%8F"><span class="nav-number">3.</span> <span class="nav-text">建造模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"><span class="nav-number">4.</span> <span class="nav-text">单例模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A3%85%E9%A5%B0%E6%A8%A1%E5%BC%8F"><span class="nav-number">5.</span> <span class="nav-text">装饰模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A7%82%E5%AF%9F%E6%A8%A1%E5%BC%8F"><span class="nav-number">6.</span> <span class="nav-text">观察模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%82%E9%85%8D%E6%A8%A1%E5%BC%8F"><span class="nav-number">7.</span> <span class="nav-text">适配模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F"><span class="nav-number">8.</span> <span class="nav-text">代理模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%AD%E4%BB%A3%E6%A8%A1%E5%BC%8F"><span class="nav-number">9.</span> <span class="nav-text">迭代模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%8A%B6%E6%80%81%E6%A8%A1%E5%BC%8F"><span class="nav-number">10.</span> <span class="nav-text">状态模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%AD%E4%BB%8B%E6%A8%A1%E5%BC%8F"><span class="nav-number">11.</span> <span class="nav-text">中介模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F"><span class="nav-number">12.</span> <span class="nav-text">原型模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A1%A5%E6%8E%A5%E6%A8%A1%E5%BC%8F"><span class="nav-number">13.</span> <span class="nav-text">桥接模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E6%A8%A1%E5%BC%8F"><span class="nav-number">14.</span> <span class="nav-text">命令模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%87%E5%BF%98%E6%A8%A1%E5%BC%8F"><span class="nav-number">15.</span> <span class="nav-text">备忘模式</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gz1234.gitee.io/2022/08/02/js%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="郭泽">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭泽">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="js设计模式 | 郭泽">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          js设计模式
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-08-02 14:54:17" itemprop="dateCreated datePublished" datetime="2022-08-02T14:54:17+08:00">2022-08-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-04-03 17:03:05" itemprop="dateModified" datetime="2023-04-03T17:03:05+08:00">2023-04-03</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/js%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">js设计模式</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="五大设计原则-S-O-L-I-D"><a href="#五大设计原则-S-O-L-I-D" class="headerlink" title="五大设计原则 S O L I D"></a>五大设计原则 S O L I D</h2><p>S-单一职责原则</p>
<ul>
<li>一个程序只做好一件事</li>
<li>如果功能过于复杂就拆分开, 每个部分保持独立</li>
</ul>
<p>O-开放封闭原则</p>
<ul>
<li>对扩展开放, 对修改封闭</li>
<li>增加需求时, 扩展新代码, 而非修改已有代码</li>
<li>这是软件设计的终极目标</li>
</ul>
<p>L-里氏置换原则</p>
<ul>
<li>子类能覆盖父类</li>
<li>父类能出现的地方子类就能出现</li>
<li>JS中使用较少 ( 弱类型 &amp; 继承使用较少 )</li>
</ul>
<p>I-接口独立原则</p>
<ul>
<li>保持接口的单一独立, 避免出现”胖接口”</li>
<li>JS中没有接口 ( TS例外 ) , 使用较少</li>
<li>类似于单一职责原则, 这里只关注接口</li>
</ul>
<p>D-依赖导致原则</p>
<ul>
<li>面向接口编程, 依赖于抽象而不依赖于具体</li>
<li>使用方只关注接口而不关注具体类的实现</li>
<li>JS中使用较少 ( 没有接口 &amp; 弱类型 )</li>
</ul>
<a id="more"></a>
<blockquote class="pullquote mindmap mindmap-lg"><ul>
<li>设计模式<ul>
<li>创建型<ul>
<li>工厂模式（工厂方法 抽象工厂 建造者模式）</li>
<li>单例模式</li>
<li>原型模式</li>
</ul>
</li>
<li>结构型<ul>
<li>适配器模式</li>
<li>装饰器模式</li>
<li>代理模式</li>
<li>外观模式</li>
<li>桥接模式</li>
<li>组合模式</li>
<li>享元模式</li>
</ul>
</li>
<li>行为型<ul>
<li>策略模式</li>
<li>模板方法模式</li>
<li>观察者模式</li>
<li>迭代器模式</li>
<li>职责链模式</li>
<li>命令模式</li>
<li>备忘录</li>
<li>中介者</li>
<li>状态模式</li>
<li>解释器模式</li>
<li>访问者模式</li>
</ul>
</li>
</ul>
</li>
</ul>
</blockquote>

<h2 id="工厂模式"><a href="#工厂模式" class="headerlink" title="工厂模式"></a>工厂模式</h2><p>普通示例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPerson</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> a = &#123;&#125;</span><br><span class="line">    a.name = name</span><br><span class="line">    a.getName =  <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.name)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> a</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> person = createPerson(<span class="string">&#x27;高江华&#x27;</span>)</span><br><span class="line">person.getName()</span><br><span class="line"><span class="built_in">console</span>.log(person.name)</span><br></pre></td></tr></table></figure>

<p>进阶示例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name</span><br><span class="line">&#125;</span><br><span class="line">Person.prototype.getName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.name)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Car</span>(<span class="params">model</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.model = model</span><br><span class="line">&#125;</span><br><span class="line">Car.prototype.getModel = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.model)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Create</span>(<span class="params">type, param</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">this</span> <span class="keyword">instanceof</span> Create)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">this</span>[type](param)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Create(type, param)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">Create.prototype = &#123;</span><br><span class="line">    person: Person,</span><br><span class="line">    car: Car</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> person = <span class="keyword">new</span> Create(<span class="string">&#x27;person&#x27;</span>, <span class="string">&#x27;高江华&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> car = Create(<span class="string">&#x27;car&#x27;</span>, <span class="string">&#x27;灰太狼&#x27;</span>)</span><br><span class="line">person.getName()</span><br><span class="line">car.getModel()</span><br></pre></td></tr></table></figure>

<p>类示例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Product</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">name</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">init</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        alert(<span class="string">&#x27;init&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">fun1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        alert(<span class="string">&#x27;fun1&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">fun2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        alert(<span class="string">&#x27;fun2&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Creator</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">create</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Product(name)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> creator = <span class="keyword">new</span> Creator()</span><br><span class="line"><span class="keyword">let</span> p = creator.create(<span class="string">&#x27;p1&#x27;</span>)</span><br><span class="line">p.init()</span><br></pre></td></tr></table></figure>

<h2 id="建造模式"><a href="#建造模式" class="headerlink" title="建造模式"></a>建造模式</h2><p>普通示例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> data = [</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;高 江华&#x27;</span>,</span><br><span class="line">        age: <span class="number">25</span>,</span><br><span class="line">        work: <span class="string">&#x27;engineer&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;灰 太狼&#x27;</span>,</span><br><span class="line">        age: <span class="number">25</span>,</span><br><span class="line">        work: <span class="string">&#x27;teacher&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;红 太狼&#x27;</span>,</span><br><span class="line">        age: <span class="number">25</span>,</span><br><span class="line">        work: <span class="string">&#x27;xxx&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">CandiDate</span> (<span class="params">param</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> candiDate = &#123;&#125;</span><br><span class="line">    candiDate.name = param.name</span><br><span class="line">    <span class="built_in">console</span>.log(param.name)</span><br><span class="line">    candiDate.age = param.age</span><br><span class="line">    candiDate.firstName = candiDate.name.split(<span class="string">&#x27; &#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">    candiDate.lastName = candiDate.name.split(<span class="string">&#x27; &#x27;</span>)[<span class="number">1</span>]</span><br><span class="line">    candiDate.work = &#123;&#125;</span><br><span class="line">    <span class="keyword">switch</span> (param.work) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;engineer&#x27;</span>:</span><br><span class="line">            candiDate.work.name = <span class="string">&#x27;工程师&#x27;</span></span><br><span class="line">            candiDate.work.description = <span class="string">&#x27;热爱编程&#x27;</span></span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;teacher&#x27;</span>:</span><br><span class="line">            candiDate.work.name = <span class="string">&#x27;老师&#x27;</span></span><br><span class="line">            candiDate.work.description = <span class="string">&#x27;乐于分享&#x27;</span></span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            candiDate.work.name = param.work</span><br><span class="line">            candiDate.work.description = <span class="string">&#x27;无&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    candiDate.work.changeWork = <span class="function"><span class="keyword">function</span> (<span class="params">work</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.name = work</span><br><span class="line">    &#125;</span><br><span class="line">    candiDate.work.changeDes = <span class="function"><span class="keyword">function</span> (<span class="params">des</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.description = des</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> candiDate</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> candiDateArr = []</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; data.length; i++)&#123;</span><br><span class="line">    candiDateArr[i] = CandiDate(data[i])</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(candiDateArr[<span class="number">0</span>])</span><br><span class="line">candiDateArr[<span class="number">0</span>].work.changeWork(<span class="string">&#x27;xxx&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(candiDateArr[<span class="number">0</span>].work)</span><br></pre></td></tr></table></figure>

<p>进阶示例:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> data = [</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;高 江华&#x27;</span>,</span><br><span class="line">        age: <span class="number">25</span>,</span><br><span class="line">        work: <span class="string">&#x27;engineer&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;灰 太狼&#x27;</span>,</span><br><span class="line">        age: <span class="number">25</span>,</span><br><span class="line">        work: <span class="string">&#x27;teacher&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        name: <span class="string">&#x27;红 太狼&#x27;</span>,</span><br><span class="line">        age: <span class="number">25</span>,</span><br><span class="line">        work: <span class="string">&#x27;xxx&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">CandiDate</span> (<span class="params">param</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> candiDate = <span class="keyword">new</span> Person(param)</span><br><span class="line">    candiDate.name = <span class="keyword">new</span> CreateName(param.name)</span><br><span class="line">    candiDate.work = <span class="keyword">new</span> CreateWork(param.work)</span><br><span class="line">    <span class="keyword">return</span> candiDate</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">param</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.name = param.name</span><br><span class="line">    <span class="built_in">this</span>.age = param.age</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">CreateName</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.wholeName = name</span><br><span class="line">    <span class="built_in">this</span>.firstName = name.split(<span class="string">&#x27; &#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">    <span class="built_in">this</span>.secondNaem = name.split(<span class="string">&#x27; &#x27;</span>)[<span class="number">1</span>]</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">CreateWork</span>(<span class="params">work</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span> (work) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;engineer&#x27;</span>:</span><br><span class="line">            <span class="built_in">this</span>.name = <span class="string">&#x27;工程师&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.description = <span class="string">&#x27;热爱编程&#x27;</span></span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">case</span> <span class="string">&#x27;teacher&#x27;</span>:</span><br><span class="line">            <span class="built_in">this</span>.name = <span class="string">&#x27;老师&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.description = <span class="string">&#x27;乐于分享&#x27;</span></span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="built_in">this</span>.name = work</span><br><span class="line">            <span class="built_in">this</span>.description = <span class="string">&#x27;无&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    CreateWork.prototype.changeWork = <span class="function"><span class="keyword">function</span> (<span class="params">work</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.name = work</span><br><span class="line">    &#125;</span><br><span class="line">    CreateWork.prototype.changeDes = <span class="function"><span class="keyword">function</span> (<span class="params">des</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.description = des</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> candiDateArr = []</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; data.length; i++)&#123;</span><br><span class="line">    candiDateArr[i] = CandiDate(data[i])</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(candiDateArr[<span class="number">0</span>])</span><br><span class="line">candiDateArr[<span class="number">0</span>].work.changeWork(<span class="string">&#x27;xxx&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(candiDateArr[<span class="number">0</span>].work)</span><br></pre></td></tr></table></figure>

<h2 id="单例模式"><a href="#单例模式" class="headerlink" title="单例模式"></a>单例模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> createSingle = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> unique = <span class="literal">null</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">single</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            a: <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (unique === <span class="literal">null</span>) &#123;</span><br><span class="line">            unique = single()</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> unique</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"><span class="keyword">let</span> a = createSingle()</span><br><span class="line"><span class="keyword">let</span> b = createSingle()</span><br><span class="line"><span class="built_in">console</span>.log(a === b)</span><br></pre></td></tr></table></figure>

<h2 id="装饰模式"><a href="#装饰模式" class="headerlink" title="装饰模式"></a>装饰模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Car</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.price = <span class="number">10</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">carLeft</span>(<span class="params">carClass</span>) </span>&#123;</span><br><span class="line">    carClass.hasHeatSeat = <span class="literal">true</span></span><br><span class="line">    carClass.price += <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">carRight</span>(<span class="params">carClass</span>) </span>&#123;</span><br><span class="line">    carClass.hasAutoMirror = <span class="literal">true</span></span><br><span class="line">    carClass.price += <span class="number">0.8</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> car = <span class="keyword">new</span> Car()</span><br><span class="line"><span class="built_in">console</span>.log(car.price)</span><br><span class="line">carLeft(car)</span><br><span class="line">carRight(car)</span><br><span class="line"><span class="built_in">console</span>.log(car.price)</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Circle</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">draw</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;画一个圆&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Decorator</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">circle</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.circle = circle</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">draw</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.circle.draw()</span><br><span class="line">        <span class="built_in">this</span>.setRedBorder(circle)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setRedBorder</span>(<span class="params">circle</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;设置红色边框&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> circle = <span class="keyword">new</span> Circle()</span><br><span class="line">circle.draw()</span><br><span class="line"><span class="keyword">let</span> dec = <span class="keyword">new</span> Decorator(circle)</span><br><span class="line">dec.draw()</span><br></pre></td></tr></table></figure>

<p>es7装饰器</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">dec</span>(<span class="params">isDec</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">target</span>) </span>&#123;</span><br><span class="line">        target.isDec = isDec</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">@dec(<span class="literal">false</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span></span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line">alert(Demo.isDec)</span><br></pre></td></tr></table></figure>

<h2 id="观察模式"><a href="#观察模式" class="headerlink" title="观察模式"></a>观察模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> msgCenter = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> msg = &#123;&#125; <span class="comment">//存消息</span></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="comment">//订消息</span></span><br><span class="line">        register: <span class="function"><span class="keyword">function</span> (<span class="params">type, fn</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (msg[type])&#123;</span><br><span class="line">                msg[type].push(fn)</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                msg[type] = [fn]</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//发消息</span></span><br><span class="line">        fire: <span class="function"><span class="keyword">function</span> (<span class="params">type, args</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (!msg[type])&#123;</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">let</span> event = &#123;</span><br><span class="line">                type: type,</span><br><span class="line">                args: args || &#123;&#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; msg[type].length; i++) &#123;</span><br><span class="line">                msg[type][i](event)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">//取消订阅消息</span></span><br><span class="line">        cancel: <span class="function"><span class="keyword">function</span> (<span class="params">type, fn</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (!msg[type]) &#123;</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; msg[type].length; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (msg[type][i] === fn) &#123;</span><br><span class="line">                    msg[type].splice(i, <span class="number">1</span>)</span><br><span class="line">                    <span class="keyword">break</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.alreadyRegister = &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line">Person.prototype.register = <span class="function"><span class="keyword">function</span> (<span class="params">type, fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">this</span>.alreadyRegister[type]) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;您已订阅,请不要重复订阅&#x27;</span>)</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        msgCenter.register(type,fn)</span><br><span class="line">        <span class="built_in">this</span>.alreadyRegister[type] = fn</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">Person.prototype.cancel = <span class="function"><span class="keyword">function</span> (<span class="params">type</span>) </span>&#123;</span><br><span class="line">    msgCenter.cancel(type, <span class="built_in">this</span>.alreadyRegister[type])</span><br><span class="line">    <span class="keyword">delete</span> <span class="built_in">this</span>.alreadyRegister[type]</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> person1 = <span class="keyword">new</span> Person()</span><br><span class="line"><span class="keyword">let</span> person2 = <span class="keyword">new</span> Person()</span><br><span class="line"><span class="keyword">let</span> person3 = <span class="keyword">new</span> Person()</span><br><span class="line">person1.register(<span class="string">&#x27;carInfo&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;person1:&#x27;</span> + e.type + e.args.info)</span><br><span class="line">&#125;)</span><br><span class="line">person1.register(<span class="string">&#x27;newInfo&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;person1:&#x27;</span> + e.type + e.args.info)</span><br><span class="line">&#125;)</span><br><span class="line">person2.register(<span class="string">&#x27;carInfo&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;person1:&#x27;</span> + e.type + e.args.info)</span><br><span class="line">&#125;)</span><br><span class="line">person3.register(<span class="string">&#x27;newInfo&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;person1:&#x27;</span> + e.type + e.args.info)</span><br><span class="line">&#125;)</span><br><span class="line">person3.cancel(<span class="string">&#x27;newInfo&#x27;</span>)</span><br><span class="line"></span><br><span class="line">msgCenter.fire(<span class="string">&#x27;carInfo&#x27;</span>, &#123;<span class="attr">info</span>: <span class="string">&#x27;新款汽车上市&#x27;</span>&#125;)</span><br><span class="line">msgCenter.fire(<span class="string">&#x27;newInfo&#x27;</span>, &#123;<span class="attr">info</span>: <span class="string">&#x27;国家领导人访华&#x27;</span>&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="适配模式"><a href="#适配模式" class="headerlink" title="适配模式"></a>适配模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Adaptee</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">specificRequest</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;德国标准插头&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Target</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.adaptee = <span class="keyword">new</span> Adaptee()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">request</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> info = <span class="built_in">this</span>.adaptee.specificRequest()</span><br><span class="line">        <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;info&#125;</span> -&gt; 转换器 -&gt; 中国标准插头`</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> target = <span class="keyword">new</span> Target()</span><br><span class="line">target.request()</span><br></pre></td></tr></table></figure>

<h2 id="代理模式"><a href="#代理模式" class="headerlink" title="代理模式"></a>代理模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ReadImg</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">filename</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.fileName = filename</span><br><span class="line">        <span class="built_in">this</span>.loadFromDisk()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">display</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;display...&#x27;</span> + <span class="built_in">this</span>.fileName)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">loadFromDisk</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;loading...&#x27;</span> + <span class="built_in">this</span>.fileName)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ProxyImg</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">fileName</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.realImg = <span class="keyword">new</span> ReadImg(fileName)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">display</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.realImg.display()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> proxyImg = <span class="keyword">new</span> ProxyImg(<span class="string">&#x27;1.png&#x27;</span>)</span><br><span class="line">proxyImg.display()</span><br></pre></td></tr></table></figure>

<p>jquery代理</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;#div1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"> <span class="built_in">setTimeout</span>($.proxy(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>, <span class="string">&#x27;yellow&#x27;</span>)</span><br><span class="line">    &#125;, <span class="built_in">this</span>), <span class="number">1000</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>es6代理</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> star = &#123;</span><br><span class="line">    name: <span class="string">&#x27;高江华&#x27;</span>,</span><br><span class="line">    age: <span class="number">25</span>,</span><br><span class="line">    phone: <span class="string">&#x27;15257184434&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> agent = <span class="keyword">new</span> <span class="built_in">Proxy</span>(star, &#123;</span><br><span class="line">    get: <span class="function"><span class="keyword">function</span> (<span class="params">target, key</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (key === <span class="string">&#x27;phone&#x27;</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&#x27;16899997777&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (key === <span class="string">&#x27;price&#x27;</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">120000</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> target[key]</span><br><span class="line">    &#125;,</span><br><span class="line">    set: <span class="function"><span class="keyword">function</span> (<span class="params">target, key, val</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (key === <span class="string">&#x27;customPrice&#x27;</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span> (val &lt; <span class="number">100000</span>)&#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;价格太低&#x27;</span>)</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                target[key] = val</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(agent.name)</span><br><span class="line"><span class="built_in">console</span>.log(agent.age)</span><br><span class="line"><span class="built_in">console</span>.log(agent.phone)</span><br><span class="line"><span class="built_in">console</span>.log(agent.price)</span><br><span class="line">agent.customPrice = <span class="number">150000</span></span><br><span class="line"><span class="built_in">console</span>.log(agent.customPrice)</span><br></pre></td></tr></table></figure>

<h2 id="迭代模式"><a href="#迭代模式" class="headerlink" title="迭代模式"></a>迭代模式</h2><ul>
<li>顺序访问一个集合</li>
<li>使用者无需知道集合内部结构</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Iterator</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">container</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.list = container.list</span><br><span class="line">        <span class="built_in">this</span>.index = <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">next</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.hasNext())&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.list[<span class="built_in">this</span>.index++]</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">hasNext</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.index &lt; <span class="built_in">this</span>.list.length;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Container</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">list</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.list = list</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">getIterator</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Iterator(<span class="built_in">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="keyword">let</span> container = <span class="keyword">new</span> Container(arr)</span><br><span class="line"><span class="keyword">let</span> iterator = container.getIterator()</span><br><span class="line"><span class="keyword">while</span> (iterator.hasNext())&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(iterator.next())</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>es6中的Iterator</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype[<span class="built_in">Symbol</span>.iterator]().next()</span><br><span class="line"><span class="comment">//原型对象下有Symbol.iterator方法执行后会得到一个迭代器</span></span><br><span class="line"><span class="comment">//迭代器的原型链上有一个next()方法</span></span><br><span class="line"><span class="comment">//执行next()方法,若value为undefined,done为true表示遍历完成</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">each</span>(<span class="params">data</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> iterator = data[<span class="built_in">Symbol</span>.iterator]()</span><br><span class="line">    <span class="keyword">let</span> item = &#123;<span class="attr">done</span>: <span class="literal">false</span>&#125;</span><br><span class="line">    <span class="keyword">while</span> (!item.done)&#123;</span><br><span class="line">        item = iterator.next()</span><br><span class="line">        <span class="keyword">if</span> (!item.done)&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(item.value)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//简化</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">each</span>(<span class="params">data</span>)</span>&#123;</span><br><span class="line">    <span class="function"><span class="title">for</span>(<span class="params"><span class="keyword">let</span> item <span class="keyword">of</span> data</span>)</span>&#123;</span><br><span class="line">     <span class="built_in">console</span>.log(item)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="keyword">let</span> m = <span class="keyword">new</span> <span class="built_in">Map</span>()</span><br><span class="line">m.set(<span class="string">&#x27;a&#x27;</span>, <span class="number">100</span>)</span><br><span class="line">m.set(<span class="string">&#x27;b&#x27;</span>, <span class="number">200</span>)</span><br><span class="line">each(arr)</span><br><span class="line">each(m)</span><br></pre></td></tr></table></figure>

<h2 id="状态模式"><a href="#状态模式" class="headerlink" title="状态模式"></a>状态模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">State</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">color</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.color = color</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">handle</span>(<span class="params">context</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.color)</span><br><span class="line">        context.setState(<span class="built_in">this</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Context</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.state = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">getState</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.state</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setState</span>(<span class="params">state</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.state = state</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> context = <span class="keyword">new</span> Context()</span><br><span class="line"><span class="keyword">let</span> green = <span class="keyword">new</span> State(<span class="string">&#x27;green&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> yellow = <span class="keyword">new</span> State(<span class="string">&#x27;yellow&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> red = <span class="keyword">new</span> State(<span class="string">&#x27;red&#x27;</span>)</span><br><span class="line">green.handle(context)</span><br><span class="line"><span class="built_in">console</span>.log(context.getState())</span><br><span class="line">yellow.handle(context)</span><br><span class="line"><span class="built_in">console</span>.log(context.getState())</span><br><span class="line">red.handle(context)</span><br><span class="line"><span class="built_in">console</span>.log(context.getState())</span><br></pre></td></tr></table></figure>

<h2 id="中介模式"><a href="#中介模式" class="headerlink" title="中介模式"></a>中介模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.number = <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setNumber</span>(<span class="params">num, m</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.number = num</span><br><span class="line">        <span class="keyword">if</span> (m)&#123;</span><br><span class="line">            m.setB()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.number = <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setNumber</span>(<span class="params">num, m</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.number = num</span><br><span class="line">        <span class="keyword">if</span> (m)&#123;</span><br><span class="line">            m.setA()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mediator</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">a, b</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.a = a</span><br><span class="line">        <span class="built_in">this</span>.b = b</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setB</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> number = <span class="built_in">this</span>.a.number</span><br><span class="line">        <span class="built_in">this</span>.b.setNumber(number * <span class="number">100</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setA</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> number = <span class="built_in">this</span>.b.number</span><br><span class="line">        <span class="built_in">this</span>.b.setNumber(number / <span class="number">100</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> a = <span class="keyword">new</span> A()</span><br><span class="line"><span class="keyword">let</span> b = <span class="keyword">new</span> B()</span><br><span class="line"><span class="keyword">let</span> m = <span class="keyword">new</span> Mediator(a, b)</span><br><span class="line">a.setNumber(<span class="number">100</span>, m)</span><br><span class="line"><span class="built_in">console</span>.log(a.number, b.number)</span><br><span class="line">b.setNumber(<span class="number">100</span>, m)</span><br><span class="line"><span class="built_in">console</span>.log(a.number, b.number)</span><br></pre></td></tr></table></figure>

<h2 id="原型模式"><a href="#原型模式" class="headerlink" title="原型模式"></a>原型模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> prototype = &#123;</span><br><span class="line">    getName:<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.first + <span class="string">&#x27; &#x27;</span> + <span class="built_in">this</span>.last</span><br><span class="line">    &#125;,</span><br><span class="line">    say:<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> x = <span class="built_in">Object</span>.create(prototype)</span><br><span class="line">x.first = <span class="string">&#x27;A&#x27;</span></span><br><span class="line">x.last = <span class="string">&#x27;B&#x27;</span></span><br><span class="line">alert(x.getName())</span><br><span class="line">x.say()</span><br><span class="line"><span class="keyword">let</span> y = <span class="built_in">Object</span>.create(prototype)</span><br><span class="line">y.first = <span class="string">&#x27;C&#x27;</span></span><br><span class="line">y.last = <span class="string">&#x27;D&#x27;</span></span><br><span class="line">alert(y.getName())</span><br><span class="line">y.say()</span><br></pre></td></tr></table></figure>

<h2 id="桥接模式"><a href="#桥接模式" class="headerlink" title="桥接模式"></a>桥接模式</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">class Color &#123;</span><br><span class="line">    constructor(name) &#123;</span><br><span class="line">        this.name &#x3D; name</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class Shape &#123;</span><br><span class="line">    constructor(name, color) &#123;</span><br><span class="line">        this.name &#x3D; name</span><br><span class="line">        this.color &#x3D; color</span><br><span class="line">    &#125;</span><br><span class="line">    draw()&#123;</span><br><span class="line">        console.log(this.color.name, this.name)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">let red &#x3D; new Color(&#39;red&#39;)</span><br><span class="line">let yellow &#x3D; new Color(&#39;yellow&#39;)</span><br><span class="line">let circle &#x3D; new Shape(&#39;circle&#39;, red)</span><br><span class="line">circle.draw()</span><br><span class="line">let triangle &#x3D; new Shape(&#39;triangle&#39;, yellow)</span><br><span class="line">triangle.draw()</span><br><span class="line">&#96;&#96;&#96;~</span><br><span class="line"></span><br><span class="line">## 策略模式</span><br><span class="line"></span><br><span class="line">&#96;&#96;&#96;js</span><br><span class="line">class User &#123;</span><br><span class="line">    constructor(type) &#123;</span><br><span class="line">        this.type &#x3D; type</span><br><span class="line">    &#125;</span><br><span class="line">    buy()&#123;</span><br><span class="line">        if (this.type &#x3D;&#x3D;&#x3D; &#39;ordinary&#39;)&#123;</span><br><span class="line">            console.log(&#39;普通用户&#39;)</span><br><span class="line">        &#125;else if (this.type &#x3D;&#x3D;&#x3D; &#39;member&#39;)&#123;</span><br><span class="line">            console.log(&#39;会员用户&#39;)</span><br><span class="line">        &#125;else if (this.type &#x3D;&#x3D;&#x3D; &#39;vip&#39;)&#123;</span><br><span class="line">            console.log(&#39;vip用户&#39;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">let u1 &#x3D; new User(&#39;ordinary&#39;)</span><br><span class="line">u1.buy()</span><br><span class="line">let u2 &#x3D; new User(&#39;member&#39;)</span><br><span class="line">u2.buy()</span><br><span class="line">let u3 &#x3D; new User(&#39;vip&#39;)</span><br><span class="line">u3.buy()</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;策略优化</span><br><span class="line">class OrdinaryUser &#123;</span><br><span class="line">    buy()&#123;</span><br><span class="line">        console.log(&#39;普通用户&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class MemberUser &#123;</span><br><span class="line">    buy()&#123;</span><br><span class="line">        console.log(&#39;会员用户&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class VipUser &#123;</span><br><span class="line">    buy()&#123;</span><br><span class="line">        console.log(&#39;vip用户&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">let u1 &#x3D; new OrdinaryUser()</span><br><span class="line">u1.buy()</span><br><span class="line">let u2 &#x3D; new MemberUser()</span><br><span class="line">u2.buy()</span><br><span class="line">let u3 &#x3D; new VipUser()</span><br><span class="line">u3.buy()</span><br></pre></td></tr></table></figure>

<h2 id="命令模式"><a href="#命令模式" class="headerlink" title="命令模式"></a>命令模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Receiver</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">exec</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;执行&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Command</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">receiver</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.receiver = receiver</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">cmd</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;执行命令&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.receiver.exec()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Invoker</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params">command</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.command = command</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">invoke</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;开始&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.command.cmd()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> soldier = <span class="keyword">new</span> Receiver()</span><br><span class="line"><span class="keyword">let</span> trumpeter = <span class="keyword">new</span> Command(soldier)</span><br><span class="line"><span class="keyword">let</span> general = <span class="keyword">new</span> Invoker(trumpeter)</span><br><span class="line">general.invoke()</span><br></pre></td></tr></table></figure>

<h2 id="备忘模式"><a href="#备忘模式" class="headerlink" title="备忘模式"></a>备忘模式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Editor</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.content = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">setContent</span>(<span class="params">content</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.content = content</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">getContent</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.content</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">saveContentToMemento</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Memento(<span class="built_in">this</span>.content)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="title">getContentFromMemento</span>(<span class="params">memento</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.content = memento.getContent()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> editor = <span class="keyword">new</span> Editor()</span><br><span class="line"><span class="keyword">let</span> careTaker = <span class="keyword">new</span> CareTaker()</span><br><span class="line">editor.setContent(<span class="string">&#x27;111&#x27;</span>)</span><br><span class="line">editor.setContent(<span class="string">&#x27;222&#x27;</span>)</span><br><span class="line">careTaker.add(editor.saveContentToMemento())</span><br><span class="line">editor.setContent(<span class="string">&#x27;333&#x27;</span>)</span><br><span class="line">careTaker.add(editor.saveContentToMemento())</span><br><span class="line">editor.setContent(<span class="string">&#x27;444&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(editor.getContent())</span><br><span class="line">editor.getContentFromMemento(careTaker.get(<span class="number">1</span>))</span><br><span class="line"><span class="built_in">console</span>.log(editor.getContent())</span><br><span class="line">editor.getContentFromMemento(careTaker.get(<span class="number">0</span>))</span><br><span class="line"><span class="built_in">console</span>.log(editor.getContent())</span><br></pre></td></tr></table></figure>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/js%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag"># js设计模式</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/08/02/git/git%E6%8F%90%E4%BA%A4%E8%A7%84%E8%8C%83/" rel="prev" title="git提交规范">
                  <i class="fa fa-chevron-left"></i> git提交规范
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/08/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/worker/" rel="next" title="web worker">
                  web worker <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
